<?php 	
/**
 * 任务系统
 */  
class productTask {
	public function index($param){
		$html = $param->h;
		Output::instance()->module("productTask",$html)->resultCode();
	}
	//首页列表
	public function getIndex($status=1,$start=0,$limit=10){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $customerId = Session::$user['customerId'];
	    $uid = Session::$user['userId'];
		Session::switchCustomer(0);
            $userTask = M("module_user_task");
		Session::switchCustomer($customerId);
		switch($status){
			case 1:
				$where['status'] = array("IN",[0,1,4]);//孟慧修改于20180409，添加 4 待审核状态
				break;
			case 2:
				$where['status'] = array("EQ",2);
				break;
			case 3:
				$where['status'] = array("EQ",3);
				break;
		}
		$start*=$limit;
		$where['uid'] = array("eq",$uid);
		$where['cId'] = array("eq",$customerId);
		$list = $userTask->where($where)->order("startTime desc,createTime desc")->field("id,proImg,progress,configJson,status,startTime,taskName")->limit($start,$limit)->select();
		if($list){
			foreach($list as $key=>$value){
				$configJson = json_decode($value['configJson'],true);
				$progress = json_decode($configJson['progress'],true);
				$list[$key]['allProgress'] = count($progress);
				$list[$key]['taskTime'] = $configJson['taskTime'];
			}
			$count = $userTask->where($where)->count("id");
			Output::instance()->objArray($list,$count);
		}else{
			Output::instance()->resultCode();
		}

	}
	//任务所有关卡列表
	public function getAllProgress($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$customerId = Session::$user['customerId'];
		$uid = Session::$user['userId'];
		Session::switchCustomer(0);
		$userTask = M("module_user_task");
        $progressModel = M("module_user_progress");
		Session::switchCustomer($customerId);
		$taskInfo = $userTask->where("id=$id")->field("id,progress,configJson,status,startTime,proImg")->find();
		$configJson = json_decode($taskInfo['configJson'],true);
		$progress = json_decode($configJson['progress'],true);
		$taskInfo['taskName'] = $configJson['taskName'];
		$taskInfo['progressBackground'] = $configJson['progressBackground'];
		$taskInfo['communityImg'] = $configJson['communityImg'];
        if($progress){
            foreach($progress as $key=>$value){
                $progress[$key]['status'] = $progressModel->where("taskId=$id and progressId=".($key+1))->getField("status");
            }
        }
		$taskInfo['progressList'] = $progress;
		$taskInfo['allProgress'] = count($progress);
		$taskInfo['taskTime'] = $configJson['taskTime'];
		Output::instance()->object($taskInfo);
	}
	//开始任务
	public function checkpointBegin($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$customerId = Session::$user['customerId'];
		$uid = Session::$user['userId'];
		Session::switchCustomer(0);
		$userTask = M("module_user_task");
		Session::switchCustomer($customerId);
		$data['startTime'] = date("Y-m-d H:i:s");
		$data['updateTime'] = date("Y-m-d H:i:s");
		$data['status'] = 1;
		$where['id'] = array("eq",$id);
		$result = $userTask->where($where)->save($data);
		if($result!==false){
			Output::instance()->object($data);
		}else{
			Output::instance()->resultCode(code_sql_error);
		}
	}
	//关卡  完成过程  （用户发的消息系统消息）
	public function getProgressInfo($taskId,$progressId){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$customerId = Session::$user['customerId'];
		$uid = Session::$user['userId'];
		Session::switchCustomer(0);
		$userTask = M("module_user_task");
		$progressModel = M("module_user_progress");
		$taskMessage = M("module_task_massage");
		Session::switchCustomer($customerId);
		$userModel = M("user_weixin_info");
		$storeModel = M("module_mall_store");
		$taskInfo = $userTask->where("id=$taskId")->field("progress,configJson,pid,uid")->find();
		$progressInfo = $progressModel->where("taskId=$taskId and progressId=$progressId")->field("descript,status,money")->find();
		if(!$progressInfo){
			$configJson = json_decode($taskInfo['configJson'],true);
			$progress = json_decode($configJson['progress'],true);
			$data = null;
			$data['taskId'] = $taskId;
			$data['progressId'] = $progressId;
			$data['cId'] = $customerId;
			$data['uid'] = $taskInfo['uid'];
			$data['pid'] = $taskInfo['pid'];
			$data['descript'] = $progress[$progressId-1]['descript'];
			$data['money'] = $progress[$progressId-1]['money'];
			$data['wish'] = $progress[$progressId-1]['wish'];
			$data['createTime'] = date("Y-m-d H:i:s");
			$data['updateTime'] = date("Y-m-d H:i:s");
			$progressModel->add($data);
			$progressInfo['descript'] = $progress[$progressId-1]['descript'];
            $progressInfo['money'] = $progress[$progressId-1]['money'];
			$data = null;
			$data['taskId'] = $taskId;
			$data['progressId'] = $progressId;
			$data['customerMassage'] = 1;
			$data['content'] = $progressInfo['descript'];
			$data['type'] = 'text';
			$data['createTime'] = date("Y-m-d H:i:s");
			$data['updateTime'] = date("Y-m-d H:i:s");
			$taskMessage->add($data);
		}
		$where['progressId'] = array("eq",$progressId);
		$where['deleted'] = array("eq",0);
		$where['taskId'] = array("eq",$taskId);
		$result['list'] = $taskMessage->where($where)->field("id,type,content,customerMassage,voiceTime")->order("updateTime asc")->select();
		$result['storeInfo'] = $storeModel->field("name,logo")->find();
		$result['userInfo'] = $userModel->where("userId=".$taskInfo['uid'])->field("nickname,headimgurl")->find();
		$result['status'] = $progressInfo['status'];
        $result['money'] = $progressInfo['money'];
		Output::instance()->object($result);
	}
	//用户发送消息  （关卡提交审核）
	public function sendMessage($taskId,$progressId,$type,$content,$voiceTime){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$customerId = Session::$user['customerId'];
		$uid = Session::$user['userId'];
		Session::switchCustomer(0);
		$taskMessage = M("module_task_massage");
		$progressModel = M("module_user_progress");
		$userTask = M("module_user_task");
		Session::switchCustomer($customerId);
		$status = $progressModel->where("taskId=$taskId and progressId=$progressId")->getField("status");
		if($status==1){
			Output::instance()->resultCode(1,'该关卡已结束');
			return;
		}
		$status = $userTask->where("id=$taskId")->getField("status");
		if(($status == 2) || ($status == 3)){//孟慧修改于20180409，添加status为4的状态之后需要兼容（原为status>1）
			Output::instance()->resultCode(1,'该任务已结束');
			return;
		}
		$data['taskId'] = $taskId;
		$data['progressId'] = $progressId;
		$data['uid'] = $uid;
		$data['customerMassage'] = 0;
		$data['content'] = $content;
		$data['type'] = $type;
		$data['voiceTime'] = $voiceTime;
		$data['createTime'] = date("Y-m-d H:i:s");
		$data['updateTime'] = date("Y-m-d H:i:s");
		$result['id'] = $taskMessage->add($data);
		$progressModel->where("taskId=$taskId and progressId=$progressId")->setField("updateTime", date("Y-m-d H:i:s"));
        $data_task['status'] = 4;//孟慧添加于20180409，用户发送信息时更改为待审核状态。（0未开始1进行中2已完成3已失败4待审核）
        $data_task['updateTime'] = date("Y-m-d H:i:s");
		$userTask->where("id=$taskId")->save($data_task);
		if($result['id']){
			Output::instance()->object($result);
		}else{
			Output::instance()->resultCode(1,'发送失败');
		}
	}
	//用户消息撤回
	public function deletedMssage($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$customerId = Session::$user['customerId'];
		$uid = Session::$user['userId'];
		Session::switchCustomer(0);
		$taskMessage = M("module_task_massage");
		Session::switchCustomer($customerId);
		$where['id'] = array("eq",$id);
		$data['deleted'] = 1;
		$data['updateTime'] = date("Y-m-d H:i:s");
		$result = $taskMessage->where($where)->save($data);
		if($result!==false){
			Output::instance()->resultCode();
		}else{
			Output::instance()->resultCode(code_sql_error);
		}
	}
}